package com.sqx.modules.address.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.address.dao.AddressDao;
import com.sqx.modules.address.entity.Address;
import com.sqx.modules.address.service.AddressService;
import com.sqx.modules.tbOrders.dao.TbOrdersDao;
import com.sqx.modules.tbOrders.entity.TbOrders;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description：TODO
 * @author： qiao
 * @create： 2024/10/15 11:03
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressDao, Address> implements AddressService {

    @Autowired
    private TbOrdersDao ordersDao;

    @Override
    public Result addAddress(Address address) {
        if (address.getAddressId() != null) {
            Address address1 = baseMapper.selectById(address.getAddressId());
            if (address1 != null) {
                if (address.getAddressDefault() == 1) {
                    address.setAddressDefault(1);
                    setNonDefaultForOthers(address1.getUserId(), address1.getAddressId());
                } else {
                    address.setAddressDefault(0);
                }
                baseMapper.updateById(address);
            }
        } else {
            List<Address> list = baseMapper.selectList(new QueryWrapper<Address>().eq("user_id", address.getUserId()));
            if (!list.isEmpty()) {
                address.setAddressDefault(address.getAddressDefault());
            } else {
                address.setAddressDefault(address.getAddressDefault());
            }
            baseMapper.insert(address);
            if (address.getAddressDefault() == 1) {
                setNonDefaultForOthers(address.getUserId(), address.getAddressId());
            }
        }
        return Result.success();
    }

    private void setNonDefaultForOthers(Long userId, Long addressId) {
        // 将用户的所有其他地址设置为非默认
        Address nonDefaultAddress = new Address();
        nonDefaultAddress.setAddressDefault(0);
        baseMapper.update(nonDefaultAddress,
                new QueryWrapper<Address>().eq("user_id", userId).ne(addressId != null, "address_id", addressId));
    }

    @Override
    public Result deleteAddress(Long userId,Long addressId) {
        Address address = baseMapper.selectById(addressId);
        if (address == null) {
            return Result.error("该地址不存在");
        }
        if (!userId.equals(address.getUserId())){
            return Result.error("您没有权限操作此地址");
        }
        if (address.getAddressDefault() == 0) {
            List<TbOrders> order = ordersDao.selectList(new QueryWrapper<TbOrders>().eq("address_id", addressId));
            if (!order.isEmpty()) {
                return Result.error("该地址正在使用中，无法删除");
            }
            baseMapper.deleteById(addressId);
            return Result.success("删除成功");
        } else if (address.getAddressDefault() == 1) {
            List<Address> address1 = baseMapper.selectList(new QueryWrapper<Address>().eq("user_id", address.getUserId()));
            boolean hasNonDefaultAddress = false;
            if (address1 != null) {
                for (Address address2 : address1) {
                    if (address2.getAddressId().equals(addressId)) {
                        continue;
                    }
                    if (address2.getAddressDefault() == 0) {
                        address2.setAddressDefault(1);
                        baseMapper.update(address2, new QueryWrapper<Address>().eq("address_id", address2.getAddressId()));
                        hasNonDefaultAddress = true;
                        break;
                    }
                }
            }
            if (!hasNonDefaultAddress) {
                return Result.error("该地址为默认地址，无法删除");
            } else {
                List<TbOrders> order = ordersDao.selectList(new QueryWrapper<TbOrders>().eq("address_id", addressId));
                if (!order.isEmpty()) {
                    return Result.error("该地址正在使用中，无法删除");
                }
                baseMapper.deleteById(addressId);
                return Result.success("默认地址已删除并设置了新的默认地址");
            }
        }
        return Result.success("删除失败");
    }

    @Override
    public Result getAddressList(Integer page, Integer limit, Address address) {
        IPage<Address> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        if (address.getUserId() != null) {
            queryWrapper.eq("user_id", address.getUserId());
            queryWrapper.like(StringUtils.isNotBlank(address.getUserName()), "user_name", "%" + address.getUserName() + "%");
            queryWrapper.like(StringUtils.isNotBlank(address.getUserPhone()), "user_phone", "%" + address.getUserPhone() + "%");
            queryWrapper.like(StringUtils.isNotBlank(address.getAddressDetail()), "address_detail", "%" + address.getAddressDetail() + "%");
        }
        return Result.success().put("data", baseMapper.selectPage(pages, queryWrapper));
    }

    @Override
    public Result selectByAddressId(Long addressId) {
        Address address = baseMapper.selectById(addressId);
        if (address != null) {
            return Result.success().put("data", address);
        }
        return Result.error("查询失败");
    }
}
